package com.lw.leetcode.mode;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author liw
 * @Date 2021/4/20 15:05
 * @Version 1.0
 */
public class GrayCode {

    public static void main(String[] args) {
        List<Integer> integers = new GrayCode().grayCode(2);
        System.out.println(integers);
    }

    public List<Integer> grayCode(int n) {
        int a = 2 << n;
        List<Integer> list = new ArrayList<>(a);
        list.add(0);
        if (n == 0) {
            return list;
        }
        int b = 1;
        for (int j = 0; j < n; j++) {

            for (int i = list.size() - 1; i >= 0; i--) {
                list.add(b + list.get(i));
            }
            b = b << 1;
        }
        return list;
    }


}
